package hdb.dao;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import hdb.om.Artikel;
import hdb.om.Vorrat;
import hdb.om.LagerTyp;

/**
 * The Data access class for locations. All Interaction with the database
 * regarding the entity bean Vorrat should be handled by this class!
 * @author juergen.schoenwaelder@stud.fh-deggendorf.de, 
 * 			thomas.eberhart@stud.fh-deggendorf.de
 */
public class LagerTypDao extends HibernateDaoSupport{
	VorratDao vorratDao;
	
	public void setVorratDao(VorratDao vorratDao) {
		this.vorratDao = vorratDao;
	}

	/**
	 * returns a single location typ by its primary db key
	 * @param id
	 * @return a single LagerTyp
	 */
	public LagerTyp findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (LagerTyp) template.get(LagerTyp.class, id);
	}
	
	/**
	 * Returns all locations from the database.
	 * @return a list of LagerTyp
	 * @see LagerTyp
	 */
	@SuppressWarnings("unchecked")
	public List<LagerTyp> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		
//		SessionFactory hibernateSessionFactory = template.getSessionFactory();
//		Session sess = hibernateSessionFactory.getCurrentSession();
//		SQLQuery sqlQuery = sess.createSQLQuery("SELECT * FROM lagertyp");
//		List lagertyp = sqlQuery.list();
		
//		Criteria crit = sess.createCriteria(LagerTyp.class);
//		List lagertyp = crit.list();
//		return lagertyp;
		
		return template.loadAll(LagerTyp.class); 
		
		
	
	}
	
	/**
	 * modifies an existing location in the database which is selected by its primary key.
	 * @param id the primary key of the item
	 * @param bezeichnung the new bezeichnung for the location
	 * @see LagerTyp
	 */
	public void modify(int id, String bezeichnung) {
		HibernateTemplate template = getHibernateTemplate();
		LagerTyp lagertyp = (LagerTyp) template.get(LagerTyp.class, id);
		lagertyp.setLagertypbezeichnung(bezeichnung);
		template.saveOrUpdate(lagertyp);
	}

	/**
	 * creates a new persistent location in the database providing all the necessary details.
	 * @param bezeichnung the new bezeichnung for the location
	 * @see LagerTyp
	 */
	public void create(String bezeichnung) {
		HibernateTemplate template = getHibernateTemplate();
		LagerTyp lagertyp = new LagerTyp(bezeichnung);
		template.persist(lagertyp);
	}
	
	/**
	 * deletes a location using it's id.
	 * @param id
	 * @throws LagerTypDeleteException if there are Vorrat objects involving the item.
	 */
	public void deleteById(int id) throws LagerTypDeleteException {
		HibernateTemplate template = getHibernateTemplate();
		LagerTyp lt = findById(id);
		Set<Vorrat> vorrat = vorratDao.findByLagerTyp(lt);
		if (vorrat == null || vorrat.size() == 0) {
			template.delete(lt);
		} else {
			throw new LagerTypDeleteException();
		}
	}
	
}
